﻿using Authority.Doamin;
using Authority.Doamin.IRepository;
using Authority.Doamin.View;
using HelperFromTheSkeleton.EFBaseRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq.Expressions;

namespace Authority.EFRepositoryImpl.EFRepositoryImpl
{
    public class RoutingAuthorityRepositoryImpl : EFBaseTreeRepositoryImpl<RoutingAuthority>, IRoutingAuthorityRepository
    {
        private readonly AuthorityDbContext _authorityDbContext;
        public RoutingAuthorityRepositoryImpl(AuthorityDbContext authorityDbContext) : base(authorityDbContext)
        {
            _authorityDbContext = authorityDbContext;
        }

        public async Task<List<RoutingView>> FindRoutingInfo(Expression<Func<RoutingAuthority, bool>> expression)
        {
            var routingCarryActionDB = _authorityDbContext.RoutingCarryAction;
            var dbinfos = await (from routing in _dbSet.Where(expression)
                                 join routingCarryAction in routingCarryActionDB on routing.ID equals routingCarryAction.RoutingAuthorityID into routingCarryActions
                                 from routingCarryActionDF in routingCarryActions.DefaultIfEmpty()
                                 join action in _authorityDbContext.ActionAuthority on routingCarryActionDF.ActionAuthorityID equals action.ID into actions
                                 from actionDF in actions.DefaultIfEmpty()
                                 select new
                                 {
                                     routing.ID,
                                     routing.ParentID,
                                     routing.Name,
                                     routing.Code,
                                     routing.Address,
                                     routing.SubsystemID,
                                     routing.DateAddTime,
                                     routing.DateUpTime,
                                     routing.IsDelete,
                                     Actiona = actionDF,
                                 }).ToListAsync();
            var dbGroupByID = dbinfos.GroupBy(x => x.ID).ToList();
            var result = new List<RoutingView>();
            foreach (var val in dbGroupByID)
            {
                var routing = val.FirstOrDefault();
                if (routing == null) continue;
                RoutingView routingView = new()
                {
                    ID = val.Key,
                    Address = routing.Address,
                    Code = routing.Code,
                    DateAddTime = routing.DateAddTime,
                    DateUpTime = routing.DateUpTime,
                    IsDelete = routing.IsDelete,
                    Name = routing.Name,
                    ParentID = routing.ParentID,
                    SubsystemID = routing.SubsystemID,
                    ActionInfo = val.Where(m=>m.Actiona!=null).Select(m=>m.Actiona).GroupBy(m=>m.ID).ToDictionary(m=>m.Key,m=>m.FirstOrDefault()?.Name??"")
                };
                result.Add(routingView);
            }
            return result;
        }
    }
}
